// MIT License (MIT)
// 
// Copyright (c) 2014 K Ercan Turkarslan
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// 

using System;
using System.Collections.Generic;
using System.Linq;
using Mios.ExtensionMethods;
using Mios.MiosConnection.JSONStructures;

namespace Mios.MiosConnection
{
    public sealed class Ui7Controllers
    {
        #region Public ReadOnly Controllers : Array of Ui7Controller;

        private readonly List<Ui7Controller> _controllers;

        public Ui7Controller[] Controllers
        {
            get { return _controllers.ToArray<Ui7Controller>(); }
        }

        #endregion

        public String AuthenticationServer { get; private set; }
        public String Username { get; protected set; }
        public String Password { get; private set; }
        public String PasswordSeed { get; private set; }
        public String PasswordHash { get; private set; }
        public String PkOem { get; private set; }

        public JsonIdentityPackage AuthenticationIdentity { get; private set; }

        #region Constructor(s)

        public Ui7Controllers(String authenticationServer, String username, String password, String passwordSeed, String pkOem)            
        {
            _controllers = new List<Ui7Controller>();

            AuthenticationServer = authenticationServer;
            Username = username;
            Password = password;
            PasswordSeed = passwordSeed;
            PasswordHash = String.Format("{0}{1}{2}", username, password, passwordSeed).Sha1Hash();
            PkOem = pkOem;

            AuthenticationIdentity = JsonIdentityPackage.GetIdentityPackage(
                String.Format(@"https://{0}/autha/auth/username/{1}?SHA1Password={2}&PK_Oem={3}", authenticationServer, username, PasswordHash, PkOem));

            var ui7Controllers = JsonUi7Controllers.GetDevices(AuthenticationIdentity.IdentityDetails.ServerAuth,
                                                               AuthenticationIdentity.IdentityDetails.PkAccount);

            foreach (var controller in ui7Controllers.Devices)
            {
                var ui7Controller = new Ui7Controller(Username, controller, AuthenticationIdentity);
                ui7Controller.GetDeviceDetail(controller.ServerDevice);

                _controllers.Add(ui7Controller);
            }
        }

        #endregion

        public Ui7Controller GetController(String engineSerial)
        {
            return _controllers.FirstOrDefault(controller => controller.EngineSerial == engineSerial);
        }
    }
}